package com.iqiyi.pps.epg.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iqiyi.kiwi.utils.Configuration;
import com.iqiyi.kiwi.utils.HttpClientConnection;
import com.iqiyi.pps.epg.core.model.stream.StreamCutterIdData;
import com.iqiyi.pps.epg.core.service.sginal.SginalLogServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * Created by zhang on 2016/3/30.
 */
public final class ApiSginalUtil {

    private static Logger logger = LoggerFactory.getLogger(ApiSginalUtil.class);

    private ApiSginalUtil() {

    }

    public static final String SGINAL_ADD = Configuration.getString("sginal.add.uri");
    public static final String SGINAL_EDIT = Configuration.getString("sginal.edit.uri");
    public static final String SGINAL_DEL = Configuration.getString("sginal.del.uri");
    public static final String SGINAL_START = Configuration.getString("sginal.start.uri");
    public static final String SGINAL_STOP = Configuration.getString("sginal.stop.uri");
    public static final String SGINAL_STATUS = Configuration.getString("sginal.status.uri");
    public static final String VERSION_GET = Configuration.getString("version.get.uri");


    public static final String RES_SUCCESS = "A00000";


    public static JSONObject add(String ip, Map<String, String> params) {
        JSONObject jsonObject = null;
        try {
            String url = "http://" + ip + SGINAL_ADD + "?id=" + params.get("channel_id");
            jsonObject = ApiHttpRequestUtil.doPostRaw(url, JSON.toJSONString(params));
            WriteSystemLogUtil.getInstance().getSginalLogService().saveLog(Integer.parseInt(params.get("channel_id")), SginalLogServiceImpl.SYSTEM_ADD,
                    url, JSON.toJSONString(jsonObject), JSON.toJSONString(params), SginalLogServiceImpl.SYSTEM_NAME);
        } catch (Exception e) {
            logger.error("[Sginal][Add][Error]", e);
        }
        return jsonObject;
    }

    public static JSONObject edit(String ip, Map<String, String> params) {
        JSONObject jsonObject = null;
        try {
            String url = "http://" + ip + SGINAL_EDIT + "?id=" + params.get("channel_id");
            jsonObject = ApiHttpRequestUtil.doPostRaw(url, JSON.toJSONString(params));
            WriteSystemLogUtil.getInstance().getSginalLogService().saveLog(Integer.parseInt(params.get("channel_id")), SginalLogServiceImpl.SYSTEM_EDIT,
                    url, JSON.toJSONString(jsonObject), JSON.toJSONString(params), SginalLogServiceImpl.SYSTEM_NAME);
        } catch (Exception e) {
            logger.error("[Sginal][Edit][Error]", e);
        }
        return jsonObject;
    }

    public static JSONObject delete(String ip, Map<String, String> params) {
        JSONObject jsonObject = null;
        try {
            String url = "http://" + ip + SGINAL_DEL;
            jsonObject = JSON.parseObject(ApiHttpRequestUtil.doGet(url, null, params));
            WriteSystemLogUtil.getInstance().getSginalLogService().saveLog(Integer.parseInt(params.get("id")), SginalLogServiceImpl.SYSTEM_DEL,
                    url, JSON.toJSONString(jsonObject), JSON.toJSONString(params), SginalLogServiceImpl.SYSTEM_NAME);
        } catch (Exception e) {
            logger.error("[Sginal][Delete][Error]", e);
        }
        return jsonObject;
    }

    public static JSONObject start(String ip, Map<String, String> params) {
        JSONObject jsonObject = null;
        try {
            String url = "http://" + ip + SGINAL_START;
            jsonObject = JSON.parseObject(ApiHttpRequestUtil.doGet(url, null, params));
            WriteSystemLogUtil.getInstance().getSginalLogService().saveLog(Integer.parseInt(params.get("id")), SginalLogServiceImpl.SYSTEM_START,
                    url, JSON.toJSONString(jsonObject), JSON.toJSONString(params), SginalLogServiceImpl.SYSTEM_NAME);
        } catch (Exception e) {
            logger.error("[Sginal][Start][Error]", e);
        }
        return jsonObject;
    }

    public static JSONObject stop(String ip, Map<String, String> params) {
        JSONObject jsonObject = null;
        try {
            String url = "http://" + ip + SGINAL_STOP;
            jsonObject = JSON.parseObject(ApiHttpRequestUtil.doGet(url, null, params));
            WriteSystemLogUtil.getInstance().getSginalLogService().saveLog(Integer.parseInt(params.get("id")), SginalLogServiceImpl.SYSTEM_STOP,
                    url, JSON.toJSONString(jsonObject), JSON.toJSONString(params), SginalLogServiceImpl.SYSTEM_NAME);
        } catch (Exception e) {
            logger.error("[Sginal][Stop][Error]", e);
        }
        return jsonObject;
    }

    public static JSONObject status(String ip, Map<String, String> params) {
        JSONObject jsonObject = null;
        try {
            String url = "http://" + ip + SGINAL_STATUS;
            jsonObject = JSON.parseObject(ApiHttpRequestUtil.doGet(url, null, params));
        } catch (Exception e) {
            logger.error("[Sginal][Status][Error]", e);
        }
        return jsonObject;
    }

    public static String getVersion(String ip) {
        String info = null;
        try {
            String url = "http://" + ip + VERSION_GET;
            info = ApiHttpRequestUtil.doGet(url, null,null);
            logger.info("[Version][GET][Success]",ip);
        } catch (Exception e) {
            logger.error("[Version][GET][Error]", e);
        }
        return info;
    }


}
